#include "plane.h"
#include "config.h"

Plane::Plane()
{
    // 初始化加载飞机图片资源
    m_plane.load(HERO_PATH);
    // 初始化飞机坐标
    m_x = (GAME_WIDTH - m_plane.width()) * 0.5;
    m_y = (GAME_HEIGHT - m_plane.height());
    // 初始化矩形边框
    m_rect.setWidth(m_plane.width());
    m_rect.setHeight(m_plane.height());
    m_rect.moveTo(m_x, m_y);
}

void Plane::shoot()
{
    // 累加时间间隔记录变量
    m_record++;
    if (m_record < BULLET_INTERVAL)
    {
        return;
    }
    // 到达发射时间
    m_record = 0;
    // 发射子弹
    for (int i = 0; i < BULLET_NUM; i++)
    {
        // 如果是空闲的子弹， 进行发射
        if (m_bullets[i].m_free)
        {
            // 将空闲状态改为假
            m_bullets[i].m_free = false;
            // 设置子弹坐标
            m_bullets[i].m_x = m_x + m_rect.width() * 0.5 - 10;
            m_bullets[i].m_y = m_y - 25;
            break;
        }
    }
}

void Plane::setPosition(int x, int y)
{
    m_x = x;
    m_y = y;
    m_rect.moveTo(m_x, m_y);
}
